﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TodayWhoModel;
using System.Web.Services;

namespace TodayWho
{
    public partial class home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSignup_OnClick(object sender, EventArgs e)
        {
            // event when user enters mail,pass and username

            TodayWhoModel.UserModel user = new UserModel();
            user.Email = txtEmail.Text.Trim();
            user.Password = txtPassword.Text.Trim();
            user.UserName = txtUserName.Text.Trim();
            user.CategoryId = Convert.ToInt32(rbListCategory.SelectedValue);
            
            if (Helper.IsKeywordinUserName(user.UserName)) // sistemde olmamasi gereken kullanici adlarini check ediyor mesaela genel,music,sport
            {
                lblErrors.Text = "Kullanıcı adı sistemde kullaniliyor. Başka bir kullanıcı adı deneyiniz.";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
            else if (!user.IsEmailUnique())
            {
                lblErrors.Text = "Email adresi sistemde kullaniliyor. Başka bir email adresi deneyiniz.";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
            else if (!user.IsUserNameUnique())
            {
                lblErrors.Text = "Kullanıcı adı sistemde kullaniliyor. Başka bir kullanıcı adı deneyiniz.";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
            else
            {
                if (user.Insert() == 0)
                {
                    lblErrors.Text = "Bir hata oluştu. Lütfen daha sonra tekrar deneyiniz.";
                    lblErrors.Style["display"] = "block";
                    errors.Style["display"] = "block";
                }
                else
                {
                    Session.Add("userId", user.UserId);
                    Session.Add("userName", user.UserName);
                    Mailer.SendWelcomeEmail(user.Email);
                    Response.Redirect("profile.aspx?u=" + user.UserName);
                }
            }

        }
        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            UserModel user = new UserModel();
            user.Email = txtEmail2.Text.Trim();
            user.Password = txtPassword2.Text.Trim();

            if (user.CheckLogin())
            {
                Session.Add("userId", user.UserId);
                Session.Add("email", user.Email);
                Session.Add("userName", user.UserName);
                Session.Add("categoryId", user.CategoryId);
                if (chkRememberMe.Checked)
                {
                    Response.SetCookie(CreateAuthCookie(user.UserName, user.Password, "salt"));
                }
                Response.Redirect("profile.aspx?u=" + user.UserName);
            }
            else
            {
                lblErrors.Text = "Email adresini ya da şifrenizi yanlış girdiniz..";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
        }
        protected void btnChangePassword_OnClick(object sender, EventArgs e)
        {
            UserModel user = new UserModel();
            user.Email = txtEmail3.Text.Trim();
            bool res = user.ChangePassword();
            if (res)
            {
                lblErrors.Text = "Şifreniz başarıyla değiştirildi. Yeni şifreniz için lütfen mail adresinizi kontrol ediniz.";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
            else
            {
                lblErrors.Text = "Bu email adresi kullanılmamaktadır. Lütfen yeniden deneyiniz.";
                lblErrors.Style["display"] = "block";
                errors.Style["display"] = "block";
            }
        }
        [WebMethod(EnableSession = true)]
        public static string FbLogin(string fbUserId, string token, string email, string name)
        {
            string res = "0";
            UserModel user = new UserModel();
            user.Email = email;
            user.NameSurname = name;
            user.FbUserId = fbUserId;
            user.FbToken = token;

            if (user.IsEmailUnique())
            {
                user.InsertFbNew();
            }
            else
            {
                user.InsertFbExisting();
            }
            user.CheckFblogin();
            HttpContext.Current.Session.Add("userId", user.UserId);
            HttpContext.Current.Session.Add("email", user.Email);
            HttpContext.Current.Session.Add("userName", user.UserName);
            HttpContext.Current.Session.Add("categoryId", user.CategoryId);
            HttpContext.Current.Session.Add("fbIsComplete", user.FbIsComplete);

            if (user.FbIsComplete)
            {

                res = "profile.aspx?u=" + user.UserName;
            }
            else
            {
                res = "fbregister.aspx";
            }

            return res;
        }

        // Create an authentication cookie that stores the username and a hash of
        // the password and salt.
        public HttpCookie CreateAuthCookie(string username, string password, string salt)
        {
            // Create the cookie and set its value to the username and a hash of the
            // password and salt. Use a pipe character as a delimiter so we can
            // separate these two elements later.
            HttpCookie cookie = new HttpCookie("todaywho");
            cookie.Value = username + "|" + Helper.CreateHash(password, salt);
            cookie.Expires = DateTime.Now.AddDays(1);
            return cookie;
        }

        
    }
}